﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace SymbolicLinkManageUI.Lib
{
    public struct My_lParam
    {
        public int i;
        public string s;
    }
    /// <summary>
    /// 使用COPYDATASTRUCT来传递字符串
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct CopyDataStruct
    {
        public IntPtr dwData;
        public int cbData;

        [MarshalAs(UnmanagedType.LPStr)]
        public string lpData;
    }
    internal class ProcessSingleControl
    {
        public const string MainWindowTitle = "MkLinkUI";
        public const int WM_APP = 0x9112;////0x8001~0xBFFF，随意#endregion Dll Imports
        public const int WM_COPYDATA = 0x004A;
        private const int SW_SHOWNOMAL = 1;


        [DllImport("User32.dll")]
        private static extern IntPtr FindWindow(string? lpClassName, string? lpWindowName);

        [DllImport("User32.dll")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

        //[DllImport("user32.dll", EntryPoint = "PostMessage")]
        //private static extern IntPtr PostMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
        //[DllImport("User32.dll", EntryPoint = "PostMessage")]
        //private static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, ref My_lParam lParam);
        [DllImport("User32.dll", EntryPoint = "PostMessage")]
        private static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, ref CopyDataStruct lParam);



        //[DllImport("User32.dll", EntryPoint = "SendMessage")]
        //private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        //[DllImport("User32.dll", EntryPoint = "SendMessage")]
        //private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref My_lParam lParam);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref CopyDataStruct lParam);


       
        /// <summary>
        /// 使程序前端显示
        /// </summary>
        /// <param name="instance"></param>
        public static bool SetForegroud(out Process instance)
        {
            if (!GetRuningInstance(out instance))
                return false;

            nint mainFormHandle = instance.MainWindowHandle;
            if (mainFormHandle != nint.Zero)
            {
                ShowWindowAsync(mainFormHandle, SW_SHOWNOMAL);
                SetForegroundWindow(mainFormHandle);
            }
            return true;
        }

        private static bool GetRuningInstance(out Process process)
        {
            process = null!;
            Process currentProcess = Process.GetCurrentProcess();
            try
            {
                Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName);
                foreach (Process m_process in Processes)
                {
                    if (m_process.Id != currentProcess.Id)
                    {
                        process = m_process;
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                try
                {
                    nint handle = FindWindow(null, "MkLinkUI");
                    if (handle == nint.Zero)
                        return false;
                    process = Process.GetProcessById((int)handle);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public static void Send(Process process, string strMsg)
        {
            if (strMsg == null) return;


            var hwnd = process.MainWindowHandle;
            if (hwnd == IntPtr.Zero) 
                return;

            CopyDataStruct cds;
            cds.dwData = IntPtr.Zero;
            cds.lpData = strMsg;
            cds.cbData = System.Text.Encoding.Default.GetBytes(strMsg).Length + 1;

            SendMessage(hwnd, WM_COPYDATA, 0, ref cds);
        }
    }
}
